package com.cyzy.controller;

import com.cyzy.dto.ResponseDto;
import com.cyzy.utils.AliOSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.util.UUID;

@RestController
public class UploadController {

    @Autowired
    private AliOSSUtil aliOSSUtil;

    @PostMapping("/upload")
    public ResponseDto upload(@RequestParam("file")MultipartFile file) throws Exception {
        //获取原始文件名
        String originalFilename = file.getOriginalFilename();
        //保证文件的名字是唯一的，从而防止文件覆盖
        String filename= UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));
        //调用阿里云oss对象存储
        String url=aliOSSUtil.uploadFile(filename,file.getInputStream());
        //返回阿里云oss文件路径
        return ResponseDto.ok(url);
    }
}
